﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Assessment.Management.SharedComponent.Entities;
using Assessment.Management.SharedComponent.Constants;
using Assessment.Management.SharedComponent.Params.ManagerParam;
using Assessment.Management.BIZ;

namespace Assessment.Management.UI.Admin.UserControls.Subjects
{
    public partial class SubjectEdit : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hdID.Value = GetParam();
                int id = int.Parse(GetParam());
                LoadDataByID(id);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            UpdateSubject();
        }
        protected void btnExit_Click(object sender, EventArgs e)
        {
           
            Response.Redirect("UniAdmin.aspx?do=subjectmanager");
        }
        
        private void UpdateSubject()
        {
            Subject subject = new Subject();
            subject.ID = int.Parse(txtMaMH.Text);
            subject.SubjectName = txtTenMH.Text.Trim();
            subject.Credit = int.Parse(txtSTC.Text.Trim());
            subject.Version = int.Parse(hdVersion.Value);
            subject.UpdatedBy = SMX.smx_UpdatedBy;
            subject.UpdatedDTG = DateTime.Now;
            SubjectParam param = new SubjectParam(FunctionType.UpdateSubject);
            param.Subject = subject;
            MainController.Provider.Execute(param);
            Response.Redirect("UniAdmin.aspx?do=subjectdisplay&id="+hdID.Value);
        }
       
        protected string GetParam()
        {
            return Request.Params["id"];
        }
        //Lấy ra sản phẩm để sửa. Đầu vào là mã sản phẩm
        private void LoadDataByID(int id)
        {
            SubjectParam param = GetSubjectToEditByID(id);
            BindItemToForm(param);
        }
        //Load dữ liệu lên form
        private void BindItemToForm(SubjectParam param)
        {
            txtMaMH.Text = GetParam();
            txtTenMH.Text = param.Subject.SubjectName;
            txtSTC.Text = param.Subject.Credit.ToString();
            hdVersion.Value = param.Subject.Version.ToString();
        }
        //Lấy ra sản phẩm để sửa. Đầu vào là mã sản phẩm
        private SubjectParam GetSubjectToEditByID(int id)
        {
            SubjectParam param = new SubjectParam(FunctionType.GetSubjectToEditByCode);
            param.Subject = new Subject() { ID = id };
            MainController.Provider.Execute(param);
            return param;
        }
    }
}


